class Solution {
public:
    int t;
    vector<bool> col, dig1, dig2;
    vector<vector<string>> ret;
    vector<string> path;

    vector<vector<string>> solveNQueens(int n) {
        t = n;
        col.resize(n + 1);
        dig1.resize(2 * n + 1);
        dig2.resize(2 * n + 1);

        path.resize(n);
        for (int i = 0; i < n; i++) path[i].append(n, '.');

        dfs(0);
        return ret;
    }
    void dfs(int r)
    {
        if (r == t)
        {
            ret.push_back(path);
            return;
        }
        string line;
        for (int i = 0; i < t; i++)
        {
            if (col[i] == false && dig1[r - i + t] == false && dig2[r + i] == false)
            {
                path[r][i] = 'Q';
                col[i] = dig1[r - i + t] = dig2[r + i] = true;
                dfs(r + 1);
                path[r][i] = '.';
                col[i] = dig1[r - i + t] = dig2[r + i] = false;
            }
        }
    }
};